﻿package com.sh.game.loader
{
    import __AS3__.vec.*;
    import flash.display.*;
    import flash.events.*;
    import flash.utils.*;

    public class ActionAssets extends Object
    {
        private var _loader:Loader;
        private var _name:String;
        private var callBack:Function;
        private var _xml:XML;
        private var _args:Array;
        private var _bytes:ByteArray;
        private var _loadInfo:LoaderInfo;
        public var frameSet:Array;
        public var frameNames:Array;
        public var processFrame:int = 0;
        private var time:int;
        public var bmps:Vector.<BitmapData>;

        public function ActionAssets(param1:ByteArray, param2:String, param3:Function, param4:Array)
        {
            this._args = param4;
            this._name = param2;
            this.callBack = param3;
            this._bytes = param1;
            return;
        }// end function

        public function get name() : String
        {
            return this._name;
        }// end function

        public function get bytes() : ByteArray
        {
            return this._bytes;
        }// end function

        public function get xml() : XML
        {
            return this._xml;
        }// end function

        public function read() : void
        {
            var swf:ByteArray;
            var size:int;
            var dic:Object;
            var i:int;
            var name:String;
            var bytes:* = this._bytes;
            this._bytes = null;
            if (bytes == null)
            {
                this.callBack.apply(null, this._args.concat(this));
                this.dispose();
                return;
            }
            try
            {
                swf = new ByteArray();
                size = bytes.readInt();
                dic = new Object();
                this.frameNames = [];
                while (size > 0)
                {
                    
                    name = bytes.readUTF();
                    this.frameNames.push(name);
                    dic[name] = {fw:bytes.readShort(), fh:bytes.readShort(), x:bytes.readShort(), y:bytes.readShort(), w:bytes.readShort(), h:bytes.readShort()};
                    size = (size - 1);
                }
                this.frameNames.sort();
                this.frameSet = new Array();
                i;
                while (i < this.frameNames.length)
                {
                    
                    this.frameSet[i] = dic[this.frameNames[i]];
                    i = (i + 1);
                }
                if (!this._loader)
                {
                    this._loader = new Loader();
                }
                bytes.readBytes(swf, 3, bytes.bytesAvailable);
                swf.position = 0;
                swf.writeUTFBytes("CWS");
                this._loader.contentLoaderInfo.addEventListener(Event.COMPLETE, this.swfLoadComplete);
                this._loader.loadBytes(swf);
                this.processFrame = this.frameNames.length;
                bytes;
                swf.clear();
            }
            catch (error:Error)
            {
                this.callBack.apply(null, _args.concat(null));
                this.dispose();
                return;
            }
            return;
        }// end function

        protected function swfLoadComplete(event:Event) : void
        {
            var _loc_2:String = null;
            this._loadInfo = event.currentTarget as LoaderInfo;
            this.bmps = new Vector.<BitmapData>;
            for each (_loc_2 in this.frameNames)
            {
                
                this.bmps.push(new this.getClass(_loc_2));
            }
            this.callBack.apply(null, this._args.concat(this));
            this._args = null;
            return;
        }// end function

        public function getClass(param1:String) : Class
        {
            var _loc_2:Class = null;
            if (this._loadInfo.applicationDomain.hasDefinition(param1))
            {
                _loc_2 = this._loadInfo.applicationDomain.getDefinition(param1) as Class;
            }
            return _loc_2;
        }// end function

        public function get isNull() : Boolean
        {
            return this._loader == null;
        }// end function

        public function dispose() : void
        {
            var _loc_1:BitmapData = null;
            if (this.callBack != null)
            {
                this.callBack = null;
            }
            this._xml = null;
            if (this._args)
            {
                this._args.length = 0;
                this._args = null;
            }
            if (this.bmps)
            {
                for each (_loc_1 in this.bmps)
                {
                    
                    _loc_1.dispose();
                }
                this.bmps = null;
            }
            this.frameNames = null;
            this.frameSet = null;
            this._name = null;
            if (this._loader)
            {
                try
                {
                    this._loader.unload();
                }
                catch (e:Error)
                {
                }
                this._loader = null;
            }
            if (this._loadInfo)
            {
                this._loadInfo = null;
            }
            return;
        }// end function

    }
}
